//
//  XMWaitMeetOrderViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/19.
//

#import "XMWaitMeetOrderViewController.h"
#import "XMWaitMeetOrderView.h"
#import "XMNetwork+mine.h"
#import "XMOrderDetailModel.h"
#import "XMApplyForReturnViewController.h"
#import "XMResellTypeSelectView.h"

#import "XMFriendListViewController.h"

@interface XMWaitMeetOrderViewController ()<XMWaitMeetOrderViewDelegate>

@property (nonatomic, strong) XMWaitMeetOrderView *waitMeetOrderView;

@property (nonatomic, strong) XMResellTypeSelectView *typeSelectView;

@property (nonatomic, strong) UITextField *priceTextField;
@property (nonatomic, strong) XMOrderDetailModel *orderDetailModel;

@property (nonatomic, copy) NSString *resellInfoStr;  //转卖信息
@property (nonatomic, copy) NSString *giveInfoStr;    //赠送信息

@end

@implementation XMWaitMeetOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.waitMeetOrderView];
    [self getOrderDetail];
}

//订单详情
- (void)getOrderDetail {
    XMWeakSelf;
    [_Network getOrderDetailWithOrderId:_orderId
                                success:^(id  _Nonnull responseObject) {
        weakSelf.orderDetailModel = [XMOrderDetailModel yy_modelWithJSON:responseObject[@"data"]];
        [weakSelf.waitMeetOrderView setContentWithOrderDetailModel:weakSelf.orderDetailModel];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//获取转卖信息
- (void)getResellInfoWithOrderId:(NSString *)orderId price:(NSString *)priceStr {
    XMWeakSelf;
    [_Network getResellInfoWithOrderId:orderId
                                 money:priceStr
                               success:^(id  _Nonnull responseObject) {
//        [self.view addSubview:self.typeSelectView];
//        [self.typeSelectView show];
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
        NSString *giveInfoStr = [responseObject safeStringForKey:@"data"];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
        [parameters safe_setObject:giveInfoStr forKey:@"giveInfo"];
        [parameters safe_setObject:self.orderDetailModel.price forKey:@"price"];
        [parameters safe_setObject:self.orderDetailModel.address forKey:@"address"];
        [parameters safe_setObject:self.orderDetailModel.serviceTime forKey:@"time"];
        [parameters safe_setObject:self.orderDetailModel.coverImageUrl forKey:@"coverImg"];
        [parameters safe_setObject:self.priceTextField.text forKey:@"payPrice"];
        [parameters safe_setObject:self.orderDetailModel.title forKey:@"title"];
        [parameters safe_setObject:@"1" forKey:@"orderType"];
        [parameters safe_setObject:userModel.nickname forKey:@"nickName"];
        [parameters safe_setObject:userModel.avatar forKey:@"avatar"];
        [parameters safe_setObject:self.orderDetailModel.serviceId forKey:@"serviceId"];
        XMFriendListViewController *friendListVC = [[XMFriendListViewController alloc] init];
        friendListVC.meetDic = parameters;
        [weakSelf.navigationController pushViewController:friendListVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//获取赠送信息
- (void)getGiveInfoWithOrderId:(NSString *)orderId {
    XMWeakSelf;
    [_Network getGiveInfoWithOrderId:orderId
                             success:^(id  _Nonnull responseObject) {
//        [self.view addSubview:self.typeSelectView];
//        [self.typeSelectView show];
        NSString *giveInfoStr = [responseObject safeStringForKey:@"data"];
        XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters safe_setObject:giveInfoStr forKey:@"giveInfo"];
        [parameters safe_setObject:self.orderDetailModel.price forKey:@"price"];
        [parameters safe_setObject:self.orderDetailModel.address forKey:@"address"];
        [parameters safe_setObject:self.orderDetailModel.serviceTime forKey:@"time"];
        [parameters safe_setObject:self.orderDetailModel.coverImageUrl forKey:@"coverImg"];
        [parameters safe_setObject:self.orderDetailModel.title forKey:@"title"];
        [parameters safe_setObject:@"2" forKey:@"orderType"];
        [parameters safe_setObject:userModel.nickname forKey:@"nickName"];
        [parameters safe_setObject:userModel.avatar forKey:@"avatar"];
        [parameters safe_setObject:self.orderDetailModel.serviceId forKey:@"serviceId"];
        XMFriendListViewController *friendListVC = [[XMFriendListViewController alloc] init];
        friendListVC.meetDic = parameters;
        [self.navigationController pushViewController:friendListVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

////获取转卖详情
//- (void)getResellDetailInfoWithContent:(NSString *)content {
//    [_Network getResellDetailInfoWitContent:content
//                                    success:^(id  _Nonnull responseObject) {
//
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
//}

//转卖分享
- (void)resellShareWithType:(NSInteger)shareType {
    NSString *shareStr = @"";
    if (_resellInfoStr.length > 0) {
        shareStr = _resellInfoStr;
    }
    else if (_giveInfoStr.length > 0) {
        shareStr = _giveInfoStr;
    }
    else {
        [MBProgressHUD showError:@"订单信息错误"];
        return;
    }
    switch (shareType) {
        case 0: {
            
        }
            break;
        case 1: {
            
        }
            break;
        case 2: {
            
        }
            break;
        case 3: {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            pab.string = @"";
            if ([pab.string isEqualToString:@""]) {
                [MBProgressHUD showSuccess:@"已复制到剪切板"];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Delegate
//联系
- (void)contactWithUserPhone:(NSString *)mobileStr {
    NSString *urlStr = [NSString stringWithFormat:@"tel://%@", mobileStr];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlStr]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:^(BOOL success) {
            
        }];
    }
}

- (void)saveWithImg:(UIImage *)img {
    UIImageWriteToSavedPhotosAlbum(img, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
}

//保存图片到本地相册
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    //4
    if (error == nil) {
        [MBProgressHUD showSuccess:@"保存成功"];
        NSLog(@"成功");
    }
    else{
        NSLog(@"失败");
    }
}

//申请退款
- (void)returnWithOrderDetailModel:(XMOrderDetailModel *)orderDetailModel {
    XMApplyForReturnViewController *applyForReturnVC = [[XMApplyForReturnViewController alloc] init];
    applyForReturnVC.orderDetailModel = orderDetailModel;
    [self.navigationController pushViewController:applyForReturnVC animated:YES];
}

//赠送
- (void)giveWithOrderId:(NSString *)orderId {
    [self getGiveInfoWithOrderId:orderId];
}

//转卖
- (void)resellWithOrderId:(NSString *)orderId serviceId:(NSString *)serviceId {
    XMWeakSelf;
    UIAlertController *alertVC = [UIAlertController initwithTitle:@"转卖价格" message:@"" titles:@[@"确认"] cancelTitle:@"取消" alertStyle:UIAlertControllerStyleAlert itemSelect:^(NSInteger selectIndex) {
        [weakSelf getResellInfoWithOrderId:orderId price:weakSelf.priceTextField.text];
    }];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入转卖价格";
        textField.font = KFontSize(14.0);
        textField.textColor = kColorHEX(0x333333);
        weakSelf.priceTextField = textField;
    }];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (XMWaitMeetOrderView *)waitMeetOrderView {
    if (!_waitMeetOrderView) {
        _waitMeetOrderView = [[XMWaitMeetOrderView alloc] initWithFrame:self.view.bounds];
        _waitMeetOrderView.delegate = self;
    }
    return _waitMeetOrderView;
}

- (XMResellTypeSelectView *)typeSelectView {
    if (!_typeSelectView) {
        _typeSelectView = [[XMResellTypeSelectView alloc] initWithFrame:self.view.bounds];
        XMWeakSelf;
        _typeSelectView.itemSelectBlock = ^(NSInteger selectIndex) {
            [weakSelf resellShareWithType:selectIndex];
        };
    }
    return _typeSelectView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
